// hyb_yl/sgin/sgin.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  // 拒绝
  jujue: function () {
    wx.navigateBack({
      delta: 1,
    })
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; // 当前页
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      maskshow: true,
      maskCont: false,
      firstmask: true,
    })
  },

  // 授权登录
  bindGetUserInfo: function (t) {
    let that = this;

    if (t.detail.userInfo) {
      app.globalData.wxInfo = t.detail.userInfo; //用户微信信息
      //获取登录信息（sessionkey，openid）
      that.getLoginInfo();
    } else {
      console.log('用户点击了“拒绝授权”')
    }
  },
  //获取登陆信息
  getLoginInfo() {
    let that = this;
    wx.login({
      success(res) {
        console.log(res)
        wx.setStorageSync('code', res.code)
        var code = res.code
        if (res.code) {
          wx.getUserInfo({
            success: function (res) {
              console.log(res)
              var encryptedData = res.encryptedData
              var iv = res.iv
              //3.解密用户信息 获取unionId
              app.util.request({
                url: "entry/wxapp/authorize.getuid",
                data: {
                  code: code,
                  encryptedData: encryptedData,
                  iv: iv
                },
                success: function (e) {
                  console.log(e)
                  wx.setStorageSync('sessionKey', e.data.userinfo.session_key)
                  app.globalData.sessionInfo = e.data.userinfo; //用户sessionkey & openid
                  // that.isLogin();
                  that.setData({
                    canIUseGetUserProfile:true
                  })
                }
              })
            },
            fail: function () {
              console.log('获取用户信息失败')
            }
          })

        }
      }
    })
  },
  getUserProfile: function () {
    var that = this;
    wx.getUserProfile({
      desc: "用于完善会员资料",
      lang: "zh_CN",
      success: function (res) {
        var userInfo = res.userInfo
        console.log(userInfo)
        var openid = app.globalData.sessionInfo.openid
        app.util.request({
          'url': 'entry/wxapp/authorize.tymember',
          'data': {
            u_name: userInfo.nickName, //昵称
            u_thumb: userInfo.avatarUrl, //头像
            u_sex: userInfo.gender, //性别
            openid: openid,
          },
          success: function (t) {
            //查询用户是否已注册过
            app.util.request({
              url: "entry/wxapp/authorize.ifregister",
              data: {
                openid: openid
              },
              success: function (resing) {
                console.log(resing)

                if (resing.data == "" || resing.data == undefined || resing.data == 'undefined' || !resing.data) {
                  app.util.request({
                    'url': 'entry/wxapp/authorize.updateadmin',
                    data: {
                      openid: openid
                    },
                    success: function (res) {
                      console.log('aaa', res)
                    }
                  })
                } else {
                  app.util.request({
                    url: "entry/wxapp/authorize.ifregister",
                    data: {
                      openid: openid
                    },
                    success: function (res) {
                      var username = res.data
                      wx.setStorage({
                        key: "myUsername",
                        data: username
                      });
                    }
                  })

                }
                var pages = getCurrentPages();
                var currPage = pages[pages.length - 1]; // 当前页
                var prevPage = pages[pages.length - 2]; // 上一个页面
                prevPage.setData({
                  maskshow: true,
                  maskCont: false,
                  firstmask: false,
                })


              }
            })

            app.globalData.userInfo = t.data; //登录成功返回用户信息
            // app.globalData.maskshow = false;
            // app.globalData.maskCont=false;
            wx.setStorageSync("openid", openid);
            wx.setStorageSync("userInfo", t.data)
            that.setData({
              maskshow: false,
              maskCont: false,
              firstmask: false,
            })
            // wx.setStorageSync('maskshow', that.data.maskshow)
            // wx.setStorageSync('maskCont', that.data.maskCont)
            that.triggerEvent('flagEvent', {}); //登录成功后继续执行
            //查询是否有公众号啊appid
            app.util.request({
              url: "entry/wxapp/authorize.ifcunzai",
              success: function (res) {
                console.log(res)
                if (res.data == 1) {
                  wx.reLaunch({
                    url: '/hyb_yl/zhuanjiasubpages/pages/webview/webview',
                  })
                } else {
                  wx.reLaunch({
                    url: '/hyb_yl/tabBar/index/index',
                  })
                }
              }
            })
          }
        })

      },
    })
  },
  isLogin() {
    let that = this;
    var openid = app.globalData.sessionInfo.openid
    var nickName = app.globalData.wxInfo.nickName
    var avatarUrl = app.globalData.wxInfo.avatarUrl
    console.log(openid)
    app.util.request({
      url: "entry/wxapp/authorize.tymember",
      data: {
        u_name: nickName,
        u_thumb: avatarUrl,
        openid: openid
      },
      success: function (t) {
        console.log(t)
        //查询用户是否已注册过
        app.util.request({
          url: "entry/wxapp/authorize.ifregister",
          data: {
            openid: openid
          },
          success: function (resing) {
            console.log(resing)

            if (resing.data == "" || resing.data == undefined || resing.data == 'undefined' || !resing.data) {
              app.util.request({
                'url': 'entry/wxapp/authorize.updateadmin',
                data: {
                  openid: openid
                },
                success: function (res) {
                  console.log('aaa', res)
                }
              })
            } else {
              app.util.request({
                url: "entry/wxapp/authorize.ifregister",
                data: {
                  openid: openid
                },
                success: function (res) {
                  var username = res.data
                  wx.setStorage({
                    key: "myUsername",
                    data: username
                  });
                }
              })

            }
            var pages = getCurrentPages();
            var currPage = pages[pages.length - 1]; // 当前页
            var prevPage = pages[pages.length - 2]; // 上一个页面
            prevPage.setData({
              maskshow: true,
              maskCont: false,
              firstmask: false,
            })


          }
        })

        app.globalData.userInfo = t.data; //登录成功返回用户信息
        // app.globalData.maskshow = false;
        // app.globalData.maskCont=false;
        wx.setStorageSync("openid", openid);
        wx.setStorageSync("userInfo", t.data)
        that.setData({
          maskshow: false,
          maskCont: false,
          firstmask: false,
        })
        // wx.setStorageSync('maskshow', that.data.maskshow)
        // wx.setStorageSync('maskCont', that.data.maskCont)
        that.triggerEvent('flagEvent', {}); //登录成功后继续执行
        //查询是否有公众号啊appid
        app.util.request({
          url: "entry/wxapp/authorize.ifcunzai",
          success: function (res) {
            console.log(res)
            if (res.data == 1) {
              wx.reLaunch({
                url: '/hyb_yl/zhuanjiasubpages/pages/webview/webview',
              })
            } else {
              wx.reLaunch({
                url: '/hyb_yl/tabBar/index/index',
              })
            }
          }
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getBase()
  },
  getBase: function () {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.base',
      success: function (res) {
        var se = res.data.ztcolor
        var baidukey = res.data.baidukey
        var bastitle = res.data.show_title
        wx.setStorageSync('bastitle', bastitle)
        wx.setStorageSync('baidukey', baidukey)
        that.setData({
          baseinfo: res.data,
          bastitle: bastitle
        })
        wx.setNavigationBarTitle({
          title: res.data.show_title
        })
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: se,
        })
        wx.setStorage({
          key: 'color',
          data: se,
        })
      },
      fail: function (err) {
        //console.log(err)
      },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})